package com.punai.customer.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.DateUtils;
import com.punai.common.utils.StringUtils;
import com.punai.customer.domain.CusVersionsDiscrepancy;
import com.punai.customer.domain.contants.CusConstants;
import com.punai.customer.mapper.CusVersionsDiscrepancyMapper;
import com.punai.customer.service.ICusVersionsDiscrepancyService;
import com.punai.standard.domain.StaVersion;
import com.punai.standard.domain.constants.StaConstants;
import com.punai.standard.mapper.StaVersionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 版本信息差异Service业务层处理
 *
 * @author zxw
 * @date 2022-12-13
 */
@Service
public class CusVersionsDiscrepancyServiceImpl extends ServiceImpl<CusVersionsDiscrepancyMapper, CusVersionsDiscrepancy> implements ICusVersionsDiscrepancyService
{
    @Autowired
    private CusVersionsDiscrepancyMapper cusVersionsDiscrepancyMapper;

    /**
     * 查询版本信息差异
     *
     * @param id 版本信息差异主键
     * @return 版本信息差异
     */
    @Override
    public CusVersionsDiscrepancy selectStaVersionsDiscrepancyById(Long id)
    {
        return cusVersionsDiscrepancyMapper.selectStaVersionsDiscrepancyById(id);
    }

    /**
     * 查询版本信息差异列表
     *
     * @param staVersionsDiscrepancy 版本信息差异
     * @return 版本信息差异
     */
    @Override
    public List<CusVersionsDiscrepancy> selectStaVersionsDiscrepancyList(CusVersionsDiscrepancy staVersionsDiscrepancy)
    {
        return cusVersionsDiscrepancyMapper.selectStaVersionsDiscrepancyList(staVersionsDiscrepancy);
    }

    /**
     * 新增版本信息差异
     *
     * @param staVersionsDiscrepancy 版本信息差异
     * @return 结果
     */
    @Override
    public int insertStaVersionsDiscrepancy(CusVersionsDiscrepancy staVersionsDiscrepancy)
    {
            return cusVersionsDiscrepancyMapper.insert(staVersionsDiscrepancy);
    }

    /**
     * 修改版本信息差异
     *
     * @param staVersionsDiscrepancy 版本信息差异
     * @return 结果
     */
    @Override
    public int updateStaVersionsDiscrepancy(CusVersionsDiscrepancy staVersionsDiscrepancy)
    {
        return cusVersionsDiscrepancyMapper.updateById(staVersionsDiscrepancy);
    }

    /**
     * 批量删除版本信息差异
     *
     * @param ids 需要删除的版本信息差异主键
     * @return 结果
     */
    @Override
    public int deleteStaVersionsDiscrepancyByIds(Long[] ids)
    {
        return cusVersionsDiscrepancyMapper.deleteStaVersionsDiscrepancyByIds(ids);
    }

    /**
     * 删除版本信息差异信息
     *
     * @param id 版本信息差异主键
     * @return 结果
     */
    @Override
    public int deleteStaVersionsDiscrepancyById(Long id)
    {
        return cusVersionsDiscrepancyMapper.deleteStaVersionsDiscrepancyById(id);
    }

    @Override
    public String getLastPublishId(String type, Long companyId) {
        String lastId = cusVersionsDiscrepancyMapper.getLastPublishId(type,companyId);
        if (StrUtil.isEmpty(lastId)) {
            return "-1";
        }
        return lastId;
    }

    @Autowired
    private StaVersionMapper staVersionMapper;

    @Override
    @Transactional
    public void publishToSta(String type, String[] specIds, Long companyId) {
        if (StrUtil.isNotEmpty(type)) {
            String name = DateUtils.dateTimeNow("yyyyMMddHHmmssSSS");
            Integer maxVerNum = staVersionMapper.getMaxVerNum(type);
            StaVersion staVersionNew = new StaVersion();
            staVersionNew.setCompanyId(companyId);
            staVersionNew.setType(type);
            staVersionNew.setName(name);
            staVersionNew.setSpecId(specIds[0]);
            staVersionNew.setVerNum(++maxVerNum);
            staVersionMapper.insert(staVersionNew);
            String versionId = staVersionNew.getId();
            String specIdsStr = CollUtil.join(CollUtil.newArrayList(specIds), ",");
            // 创建新版本成功
            if(StringUtils.equals(type, StaConstants.FB_TYPE)){
                cusVersionsDiscrepancyMapper.publishFbToSta(specIdsStr,companyId,versionId);
            } else if (StringUtils.equals(type, StaConstants.GX_TYPE)) {
                cusVersionsDiscrepancyMapper.publishGxToSta(specIdsStr,companyId,versionId);
            } else {
                throw new ServiceException("类型错误");
            }
        }
    }

    @Override
    @Transactional
    public void recordDifference(String versionId, String type, String specId, Long companyId) {
        if (StrUtil.isNotEmpty(type)) {
            switch (type) {
                case CusConstants.FB_TYPE:
                    // 获取专业
                    cusVersionsDiscrepancyMapper.recordFbDifference(specId,companyId,versionId);
                    break;
                case CusConstants.CL_TYPE:
                    cusVersionsDiscrepancyMapper.recordClDifference(companyId,versionId);
                    break;
                case CusConstants.JX_TYPE:
                    cusVersionsDiscrepancyMapper.recordJxDifference(companyId,versionId);
                    break;
                case CusConstants.GB_TYPE:
                    cusVersionsDiscrepancyMapper.recordGxDifference(specId,companyId,versionId);
                    break;
                default:
                    throw new ServiceException("无效发布类型!");
            }
        }
    }

    @Override
    public List<CusVersionsDiscrepancy> queryContrast(String type, String opType, String specIds, Long companyId) {
        List<String> types = new ArrayList<>();
        types.add(type);
        if (StrUtil.equals(CusConstants.JX_TYPE,type)) {
            types.add(CusConstants.JX_TYPE);
        }
        return cusVersionsDiscrepancyMapper.queryContrast(types,opType,specIds,companyId);
    }

    @Override
    public List<CusVersionsDiscrepancy> queryContrast(String type, String opType, Long companyId) {
        return cusVersionsDiscrepancyMapper.queryContrastTwo(type,opType,companyId);
    }

}
